<route lang="jsonc">{
  "style": {
    "navigationBarTitleText": "实名认证"
  }
}</route>

<script lang="ts" setup>
import { ref } from 'vue'
import { identityVerify, useVerify } from '@/api/user'
import ImageUpload from '@/components/ImageUpload.vue'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()
const submitting = ref(false)
const loading = ref(false)

const form = ref({
  name: '',
  identifyId: '',
  identifyFront: '',
  identifyBack: '',
})

// 获取身份信息回显
async function getVerifyInfo() {
  try {
    loading.value = true
    const res = await useVerify()
    form.value = res.data
    console.log(form.value)
  }
  catch (error) {
    console.error('获取身份信息失败:', error)
  }
  finally {
    loading.value = false
  }
}
// 页面加载时获取身份信息
onLoad(() => {
  console.log('2000000000000000000000000')
  getVerifyInfo()
})

async function handleSubmit() {
  // 表单验证
  if (!form.value.name) {
    uni.showToast({
      title: '请输入真实姓名',
      icon: 'none',
    })
    return
  }

  if (!form.value.identifyId) {
    uni.showToast({
      title: '请输入身份证号',
      icon: 'none',
    })
    return
  }

  // 身份证号格式验证
  const idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}([\dX])$)/i
  if (!idCardRegex.test(form.value.identifyId)) {
    uni.showToast({
      title: '请输入正确的身份证号',
      icon: 'none',
    })
    return
  }

  if (form.value.identifyFront.length === 0) {
    uni.showToast({
      title: '请上传身份证人像面',
      icon: 'none',
    })
    return
  }

  if (form.value.identifyBack.length === 0) {
    uni.showToast({
      title: '请上传身份证国徽面',
      icon: 'none',
    })
    return
  }

  try {
    submitting.value = true
    // 获取用户ID
    const userId = userStore.userInfo.id
    // 调用身份验证接口
    await identityVerify(userId, form.value)

    uni.showToast({
      title: '提交成功',
      icon: 'success',
    })

    // 延迟返回上一页
    setTimeout(() => {
      uni.navigateBack()
    }, 1500)
  }
  finally {
    submitting.value = false
  }
}
</script>

<template>
  <view class="container">
    <view class="form-wrapper">
      <view class="form-item">
        <view class="label">
          真实姓名
        </view>
        <view class="input-wrapper">
          <wd-input v-model="form.name" clearable placeholder-style="color: #c0c4cc" no-border
            placeholder="请输入您的身份证姓名" />
        </view>
      </view>
      <view class="form-item">
        <view class="label">
          身份证号
        </view>
        <view class="input-wrapper">
          <wd-input v-model="form.identifyId" clearable placeholder-style="color: #c0c4cc" no-border
            placeholder="请输入您的身份证号码" />
        </view>
      </view>
      <view class="form-item">
        <view class="label">
          身份证照片
        </view>
        <view class="uploader-wrapper">
          <ImageUpload v-model="form.identifyFront" upload-text="上传人像面" upload-hint="点击上传身份证正面"
            icon-type="camera-filled" />
        </view>
        <view class="uploader-wrapper">
          <ImageUpload v-model="form.identifyBack" upload-text="上传国徽面" upload-hint="点击上传身份证背面"
            icon-type="medal-filled" />
        </view>
      </view>
    </view>

    <view class="submit-button-wrapper">
      <wd-button block custom-class="submit-button" :loading="submitting" @click="handleSubmit">
        提交
      </wd-button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.container {
  background-color: #f7f7f7;
  min-height: 100vh;
  padding: 1rem;
}

.form-wrapper {
  background-color: white;
  padding: 1rem;
  border-radius: 8px;
}

.form-item {
  margin-bottom: 1rem;

  .label {
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    color: #333;
  }

  .input-wrapper {
    border: 1px dashed #e0e0e0;
    border-radius: 4px;
    padding: 0 0.75rem;
    height: 44px;
    display: flex;
    align-items: center;
  }
}

:deep(.upload-placeholder) {
  width: 100% !important;
}

.uploader-wrapper {
  width: 100%;
  margin-top: 1rem;
}

.submit-button-wrapper {
  margin-top: 2rem;
}

:deep(.submit-button) {
  background-color: #ff770d !important;
  border-color: #ff770d !important;
  color: #fff !important;
  font-size: 30rpx !important;
  font-weight: bold !important;
  border-radius: 9999px !important;
  height: 44px !important;
}

:deep(.wd-input) {
  width: 100%;
  --wd-input-font-size: 14px;
}
</style>
